﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ViewCart.ascx.cs" Inherits="freecart.Controls.ViewCart" %>
<script type="text/javascript" language="javascript">
    jQuery.noConflict();
    jQuery(document).ready(function() {        
        jQuery("button#btnDelete").live("click", function(event) {
            event.preventDefault();
            if (confirm("Are you sure you want to remove these items?") == true) {
                var button = this;
                var objItems = jQuery("form").find("input:checked").not("#__VIEWSTATE,#__EVENTVALIDATION");
                var stringify = null;
                jQuery(objItems).each(function() {
                    if (stringify == null) {
                        stringify = "ItemId=" + jQuery(this).parent().parent().find("span.IdStore").text();
                    } else {
                        stringify += "&ItemId=" + jQuery(this).parent().parent().find("span.IdStore").text();
                    }
                });
                if (stringify != null) {
                    FreecartService("RemoveItem", stringify, button);
                    if (jQuery(objItems).length > 0) {
                        RedrawPanel('UpdatePanel2');
                    }
                }
            }
        })

        jQuery(".txtQuantity").live("blur", function(event) {
            event.preventDefault();
            id = jQuery(this).parent().parent().find("span.IdStore").text();
            if (jQuery(this).val() == "0") { // remove if quantity equals zero
                if (confirm("Are you sure you want to remove these items?") == true) {
                    FreecartService("RemoveItem", "ItemId=" + id, null);
                }
            } else { // otherwise, update quantity
                FreecartService("UpdateQuantity", "ItemId=" + id + "&Quantity=" + jQuery(this).val(), null);
            }
            if (typeof (id) != undefined) {
                RedrawPanel('UpdatePanel2');
            }
        });

    });
</script>
<asp:Panel ID="ShoppingCart" runat="server">
<asp:GridView ID="GridView1" AllowSorting="true" AlternatingRowStyle-BackColor="#cccccc" BorderColor="#cccccc" AllowPaging="false" ShowFooter="true" AutoGenerateColumns="false" DataKeyNames="ItemId" runat="server">
    <Columns>
        <asp:TemplateField ItemStyle-Width="50px" HeaderText="" FooterStyle-Font-Bold="True">
            <ItemTemplate>
                <center>
                    <asp:CheckBox CssClass="SelectedForDelete" ID="chkSelect" runat="server" />
                    <span class="IdStore" style="display: none;"><%# Eval("ItemId") %></span>
                </center>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField ItemStyle-Width="50px" DataField="ItemId" HeaderText="ItemId" />
        <asp:TemplateField ItemStyle-Width="50px" HeaderText="Quantity">
            <ItemTemplate>
                <input type="text" class="txtQuantity" value="<%# Eval("Quantity")%>"></input>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ItemStyle-Width="400px" HeaderText="Product">
            <ItemTemplate>
                <div><%# Eval("Product") %></div>
                <div><i>(Options:)</i></div>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField ItemStyle-Width="100px" DataField="UnitPrice" HeaderText="Unit Price" FooterText="Total" />
        <asp:TemplateField ItemStyle-Width="50px" HeaderText="Total" FooterStyle-Font-Bold="True">
            <ItemTemplate>
                $<%# GetUnitPrice(decimal.Parse(Eval("UnitPrice").ToString()), int.Parse(Eval("Quantity").ToString())).ToString("N2")%>
            </ItemTemplate>
            <FooterTemplate>
                $<%# GetTotal().ToString("N2") %>
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<div>
    <span><button id="btnDelete">Delete Items</button></span>
    <span>&nbsp;<asp:Button ID="btnCheckout" runat="server" Text="Checkout" /></span>
</div>
<asp:DataList ID="DataList1" runat="server">
        <HeaderTemplate>
            <table>
                <thead>
                    <td>&nbsp;</td>
                    <td>Quantity</td>
                    <td>Item ID</td>
                    <td>Product</td>
                    <td>Cost</td>
                    <td>Total</td>
                </thead>
        </HeaderTemplate>
       <ItemTemplate>
                <tr>
                    <td></span></td>
                    <td><asp:TextBox ID="txtQuantity" CssClass="txtQuantity" Text='<%# Eval("Quantity") %>' runat="server"></asp:TextBox></td>
                    <td><a href="/?id=<%# Eval("ItemId") %>"><asp:Label ID="Label1" runat="server" Text='<%# Eval("ItemId") %>'></asp:Label></a></td>
                    <td><asp:Label ID="Label2" runat="server" Text='<%# Eval("Product") %>'></asp:Label></td>
                    <td><asp:Label ID="Label4" runat="server" Text='<%# Eval("UnitPrice") %>'></asp:Label></td>
                </tr>
       </ItemTemplate>
       <FooterTemplate>
            </table>
       </FooterTemplate>
    </asp:DataList>  
</asp:Panel>  
